//
//  poverAnimated.swift
//  SinaProject
//
//  Created by guochong on 16/4/22.
//  Copyright © 2016年 吴学宇. All rights reserved.
//

import UIKit

class poverAnimated: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{

     var isPresent :Bool = false;
    
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        
        return PoverPresentationController(presentedViewController: presented, presentingViewController: presenting);
        
        
    }
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        
        isPresent = true;
        
        return self;
        
    }
    
    func  animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        
        isPresent = false;
        return self;
        
    }
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        
        return 0.5;
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        
        if isPresent {
            
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey);
            
            toView?.transform = CGAffineTransformMakeScale(1, 0);
            toView?.layer.anchorPoint = CGPointMake(0.5, 0);
            transitionContext.containerView()?.addSubview(toView!);
            [UIView .animateWithDuration(0.25, animations: {
                
                
                toView?.transform = CGAffineTransformIdentity;
                
                }, completion: { (_) in
                    
                    transitionContext.completeTransition(true);
                    
            })];
            
            
        }else{
            
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey);
            
            
            [UIView .animateWithDuration(0.25, animations: {
                fromView?.transform = CGAffineTransformMakeScale(1, 0.0000000001);
                
                
                }, completion: { (_) in
                    
                    
                    transitionContext.completeTransition(true);
            })];
            
        }
        
        
        
        
    }
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
